// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/**
 * @brief
 *
 * @version  0.1
 * @author   yuanchenxi
 * @date     2024.12.20
 */
#include "TABLED2Parser.h"

#include <algorithm>
#include <cstdint>

#include <boost/algorithm/string.hpp>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Table/ITABLED2Service.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (TABLED2);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/TABLED2/TABLED2.md>
*/
// 以下需要修改
enum TABLED2Index : std::uint8_t
{
    TABLED2Index_TID    = 1,
    TABLED2Index_X1     = 2,
    TABLED2Index_XYLIST = 11,
};
SG::DataStructure::Common::Status BDF::BULKDATA::ParseTABLED2 (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                               std::vector<std::string>&                               dividedEntryInfo)
{
    // 初始化临时的TABLED2数据结构，用于存储解析后的数据
    SG::DataStructure::FEM::TABLED2Data tempTABLED2;

    // TODO(bug 001) m_dominId的正确填写
    tempTABLED2.m_domainId = 1;

    for (size_t curIDIndex = 1; curIDIndex < 3; curIDIndex++)
    {
        auto       curStrIndex = dividedEntryInfo[curIDIndex];
        bool const isEmpty     = is_all_space (curStrIndex);

        // 如果当前字符串为空白，则跳过并处理下一个
        if (isEmpty)
        {
            continue;
        }

        // 根据当前词条的位置进行相应的解析
        switch (curIDIndex)
        {
        case TABLED2Index_TID:  // 节点ID
            tempTABLED2.m_id = convertToI64 (curStrIndex);
            break;
        case TABLED2Index_X1:
            tempTABLED2.m_X1 = convertToReal (curStrIndex);
            break;
        default:
            break;
        }
    }

    const size_t rowNum = dividedEntryInfo.size () / 10;

    for (size_t curRow = 1; curRow < rowNum; curRow++)
    {
        for (size_t fieldsColumn = 1; fieldsColumn < 9; fieldsColumn += 2)
        {
            // 计算当前字段的索引
            const size_t curIndex    = curRow * 10 + fieldsColumn;
            std::string& curStrIndex = dividedEntryInfo[curIndex];
            boost::trim (curStrIndex);
            if (curStrIndex.size () == 0)
            {
                continue;
            }
            // 发现skip 继续循环
            if (curStrIndex[0] == 'S' || curStrIndex[0] == 's')
            {
                continue;
            }
            // 发现endt 跳出循环
            if (curStrIndex[0] == 'E' || curStrIndex[0] == 'e')
            {
                break;
            }
            tempTABLED2.m_XYList.push_back ({ convertToReal (curStrIndex), convertToReal (dividedEntryInfo[curIndex + 1]) });
        }
    }

    // 将解析后的TABLED2数据添加到数据库服务中
    return dbServiceFactory->get<ITABLED2Service> ()->Add (tempTABLED2);
}